package com.haowang.camera.manager;

import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

import com.haowang.camera.R;
import com.haowang.camera.activity.CameraActivity;
import com.haowang.camera.util.GlobalValues;
import com.haowang.camera.util.Util;

public class SurfaceHorderPreviewManager extends PreviewManager implements
		SurfaceHolder.Callback {

	private static final String TAG = SurfaceHorderPreviewManager.class
			.getSimpleName();
	private SurfaceHolder mSurfaceHolder;
	private SurfaceView mSurfaceView = null;

	public SurfaceHorderPreviewManager(CameraActivity mActivity) {
		super(mActivity);
		mSurfaceView = (SurfaceView) mActivity
				.findViewById(R.id.preview_surface_view);
		mSurfaceHolder = mSurfaceView.getHolder();
		mSurfaceHolder.addCallback(this);
		mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}

	@Override
	public void init() {
		setLayout(GlobalValues.MODE_PHOTO);
		super.init();
	}
	@Override
	public void setLayout(int mode) {
		RelativeLayout.LayoutParams lp = (LayoutParams) mSurfaceView
				.getLayoutParams();
		switch (mode) {
		case GlobalValues.MODE_SQUARE:
			lp.width = Util.getDisplayWith();
			lp.height = Util.getDisplayWith();
			break;

		default:
			lp.width = Util.getDisplayWith();
			lp.height = Util.getDisplayWith() * 4 / 3;
			break;
		}
		lp.topMargin = (int) ((Util.getDisplayHeight() - lp.height) * (1 - 0.618));
		mBottomMargin = (int) ((Util.getDisplayHeight() - lp.height) * 0.618);
		mSurfaceView.setLayoutParams(lp);
	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		Log.i(TAG, "surfaceChanged");
		if (mSurfaceHolder.getSurface() == null) {
			return;
		}

		// stop preview before making changes
		try {
			CameraManager.instance().stopPreview();
		} catch (Exception e) {
			// ignore: tried to stop a non-existent preview
		}

		// set preview size and make any resize, rotate or
		// reformatting changes here
		/* TODO */

		// start preview with new settings
		try {
			mActivity.getCameraModule().getCameraDevice()
					.setPreviewDisplay(mSurfaceHolder);
			CameraManager.instance().startPreview();

		} catch (Exception e) {
			Log.d(TAG, "Error starting camera preview: " + e.getMessage());
		}

	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		Log.i(TAG, "surfaceCreated");
		try {
			Thread thread = mActivity.getCameraModule()
					.getCameraStartUpThread();
			if (thread != null && thread.isAlive()) {
				thread.join();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			mActivity.getCameraModule().getCameraDevice()
					.setPreviewDisplay(mSurfaceHolder);
			CameraManager.instance().startPreview();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		Log.i(TAG, "surfaceDestroyed");
		// nothing
	}
}
